{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-06-12T13:16:52.624415800Z",
     "start_time": "2024-06-12T13:16:52.387841400Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    Name  Age\n",
      "0  Alice   25\n",
      "1    Bob   30\n",
      "    Name  Salary\n",
      "0  Alice   50000\n",
      "1    Bob   60000\n"
     ]
    }
   ],
   "source": [
    "# 根据键合并\n",
    "df1 = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [25, 30]})\n",
    "df2 = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Salary': [50000, 60000]})\n",
    "\n",
    "print(df1)\n",
    "print(df2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-06-12T13:16:52.634730500Z",
     "start_time": "2024-06-12T13:16:52.626415900Z"
    }
   },
   "id": "b16d3cd3b3d233a2"
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    Name  Age  Salary\n",
      "0  Alice   25   50000\n",
      "1    Bob   30   60000\n"
     ]
    }
   ],
   "source": [
    "df = pd.merge(df1, df2, on='Name')\n",
    "print(df)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-06-12T13:16:52.642656500Z",
     "start_time": "2024-06-12T13:16:52.630729Z"
    }
   },
   "id": "5808edd7bc9ba768"
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始 DataFrame:\n",
      "   id     name  age\n",
      "0   1    Alice   24\n",
      "1   2      Bob   27\n",
      "2   3  Charlie   22\n",
      "3   4    David   32\n",
      "\n",
      "找到的行索引: 1\n",
      "\n",
      "修改后的 DataFrame:\n",
      "   id     name  age\n",
      "0   1    Alice   24\n",
      "1   2   Robert   28\n",
      "2   3  Charlie   22\n",
      "3   4    David   32\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 创建一个示例 DataFrame\n",
    "data = {\n",
    "    'id': [1, 2, 3, 4],\n",
    "    'name': ['Alice', 'Bob', 'Charlie', 'David'],\n",
    "    'age': [24, 27, 22, 32]\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "print(\"原始 DataFrame:\")\n",
    "print(df)\n",
    "\n",
    "# 查找 'id' 为 2 的行\n",
    "row_index = df[df['id'] == 2].index[0]\n",
    "print(\"\\n找到的行索引:\", row_index)\n",
    "\n",
    "# 修改 'name' 和 'age' 列的数据\n",
    "df.at[row_index, 'name'] = 'Robert'\n",
    "df.at[row_index, 'age'] = 28\n",
    "\n",
    "print(\"\\n修改后的 DataFrame:\")\n",
    "print(df)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-06-12T13:16:52.681097500Z",
     "start_time": "2024-06-12T13:16:52.640655900Z"
    }
   },
   "id": "8c1922a062d3cfcc"
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   id     name  age\n",
      "0   1    Alice   24\n",
      "1   2      Bob   27\n",
      "2   3  Charlie   22\n",
      "3   4    David   32\n",
      "   id     name  age\n",
      "0   4    David   32\n",
      "1   3  Charlie   22\n",
      "2   2      Bob   27\n",
      "3   1    Alice   24\n"
     ]
    }
   ],
   "source": [
    "from copy import deepcopy\n",
    "data = {\n",
    "    'id': [1, 2, 3, 4],\n",
    "    'name': ['Alice', 'Bob', 'Charlie', 'David'],\n",
    "    'age': [24, 27, 22, 32]\n",
    "}\n",
    "df_test = pd.DataFrame(data)\n",
    "print(df_test)\n",
    "df_test = df_test.iloc[::-1].reset_index(drop=True)\n",
    "print(df_test)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-06-12T13:16:52.681097500Z",
     "start_time": "2024-06-12T13:16:52.653721400Z"
    }
   },
   "id": "7eac52f26464de8a"
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id,name,age\r\n",
      "1,Alice,24\r\n",
      "2,Bob,27\r\n",
      "3,Charlie,22\r\n",
      "4,David,32\r\n"
     ]
    }
   ],
   "source": [
    "data = {\n",
    "    'id': [1, 2, 3, 4],\n",
    "    'name': ['Alice', 'Bob', 'Charlie', 'David'],\n",
    "    'age': [24, 27, 22, 32]\n",
    "}\n",
    "df_test = pd.DataFrame(data)\n",
    "\n",
    "# 将 DataFrame 转换为 CSV 格式的字符串\n",
    "csv_string = df_test.to_csv(index=False)\n",
    "print(csv_string)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-06-12T13:16:52.682098300Z",
     "start_time": "2024-06-12T13:16:52.660773400Z"
    }
   },
   "id": "df7f784f8e87f71e"
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-06-12T13:16:52.682098300Z",
     "start_time": "2024-06-12T13:16:52.671591900Z"
    }
   },
   "id": "cb1a8a36a2102ce5"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
